<?php
namespace app\shop\controller;
use think\Controller;
use think\request;
use think\Db;
use app\service\lifeapiService;//生活号接口
use app\service\shopService;
use app\service\appletService;
use app\shop\controller\Common;
use printing\printShop;
use think\Log;
class Bill extends Common{
	
	//账单详情
	public function details(){
		session("url",request()->url());
		
		$userinfo=Db::table('zz_user')->where('user_id',session('user_id'))->find();
		//未绑定房屋
		if($userinfo["out_room_id"]=="0"){
			header("Location:https://www.jfzhiwen.com/shop/room/bind");
			return ;
		}
		
		$user['user_id'] = session('user_id');//user_id
		
		$billlis = (new appletService())->billRoomIdList($userinfo['out_room_id']);
		//$billlis= Db::name('bill')->where('out_room_id',$userinfo['out_room_id'])->where('status','WAIT_PAYMENT')->where('id','>','261216')->select();
		if(empty($billlis)){
			return View::fetch("nopaymsg");
		}
		$billlist = (new appletService())->cost_type($userinfo['out_room_id']);
		
		/*foreach ($billlist as $b){
			$lis[] = (new appletService())->select($userinfo['out_room_id'],$b);
		}*/
		$roominfo=Db::table("roominfo")->where("out_room_id",$userinfo["out_room_id"])->find();
		//var_dump($roominfo);die;
		$this->assign("userinfo",$userinfo);
		$this->assign("billlist",$billlis);
		$this->assign("roominfo",$roominfo);
		return View::fetch();
	}
	//计算要缴费的金额
	public function moneysum(){
		$data["point_status"]=request()->has('point_status')?"1":"0";
		if(!(request()->has('billid'))){
			return json(['state'=>'0','info' => '请选择要交费的账单']);
		}
		$post=request()->post();
		$data["point"]=0;
		if($data["point_status"]=="1"){
			$data["point"]=$post["point"];
		}
		$ordermoney=0;
		foreach ($post["billid"] as $key => $value) {
			$billinfo=Db::table("bill")->where('id',$value)->find();
			$ordermoney=$ordermoney+$billinfo["bill_entry_amount"];
		}
		$data["order_money"]=$ordermoney;
		$data["point_money"]=round($data["point"]/100,2);//优惠金额
		if($data["point_money"]>=$data["order_money"]){
			return json(['state'=>'0','info' => '缴费金额必须大于0']);
		}
		$data["pay_money"]=round($data["order_money"]-$data["point_money"],2);
		return json(['state'=>'1','info' => $data["pay_money"]]);
	}

	public function createOrder(){

		$userinfo=Db::table('zz_user')->where('user_id',session('user_id'))->find();

		$data["point_status"]=request()->has('point_status')?"1":"0";
		if(!(request()->has('billid'))){
			return json(['info' => '请选择要交费的账单']);
		}
		$post=request()->post();
		$data["bill_id"]=serialize($post["billid"]);
		$data["point"]=0;
		if($data["point_status"]=="1"){
			$data["point"]=$post["point"];
		}
		

		$ordermoney=0;
		foreach ($post["billid"] as $key => $value) {
			$billinfo=Db::table("bill")->where('id',$value)->find();
			$ordermoney=$ordermoney+$billinfo["bill_entry_amount"];
		}
		$data["order_money"]=$ordermoney;
		$data["buyer_id"]=$userinfo["user_id"];
		
		$data["out_room_id"]=$userinfo["out_room_id"];
		$data["room_address"]=$userinfo["room_address"];
		$data["community_id"]=$userinfo["community_id"];
		$data["community_name"]=$userinfo["community_name"];
		$data["point_money"]=round($data["point"]/100,2);//优惠金额
		if($data["point_money"]>=$data["order_money"]){
			return json(['info' => '缴费金额必须大于0']);
		}
		$data["pay_money"]=round($data["order_money"]-$data["point_money"],2);
		$data["out_order_no"]=date('YmdHiss').rand(100000,999999);

		$api = new lifeapiService();
		$token=(new shopService())->communityToken($data["community_id"]);
		$notifyurl='https://www.jfzhiwen.com/shop/ali/billnotice';
		$result = $api->orderCreate(json_encode(['out_trade_no' =>$data['out_order_no'], 'total_amount' =>  $data['pay_money'], 'buyer_id' =>  $data['buyer_id'],'subject' => $data["community_name"].$data["room_address"].'缴费', 'token' => $token,'notifyurl' => $notifyurl]));
		if(property_exists($result,'sub_msg')){	
			return json(['info' => $result->sub_msg]);
		}else{
			$data['trade_no'] =  $result->trade_no;
			$data["logon_id"]=$userinfo["logon_id"];
			$data["user_name"]=$userinfo["user_name"];	
			$res=Db::table("zz_bill_order")->insert($data);
			//返回结果到前台唤起收银台
			if($res){
				return json(['order_no' => $data['trade_no']]);
			}else{
				return json(['info' => '订单存储失败']);
			}
		}
		
	}
	/*public function test(){
		$userinfo=Db::table('zz_user')->where('user_id',session('user_id'))->find();

		$data["point_status"]=request()->has('point_status')?"1":"0";
		$post=request()->post();
		$data["bill_id"]=serialize($post["billid"]);
		$data["point"]=0;
		if($data["point_status"]=="1"){
			$data["point"]=$post["point"];
		}
		$data["order_money"]=array_sum($post["ordermoney"]);
		$data["buyer_id"]=$userinfo["user_id"];
		$data["logon_id"]=$userinfo["logon_id"];
		$data["user_name"]=$userinfo["user_name"];
		$data["out_room_id"]=$userinfo["out_room_id"];
		$data["room_address"]=$userinfo["room_address"];
		$data["community_id"]=$userinfo["community_id"];
		$data["community_name"]=$userinfo["community_name"];
		$data["discount_money"]=$data["point"]/100;//优惠金额
		$data["receipt_money"]=$data["order_money"]-$data["discount_money"];
		$data["out_order_no"]=date('YmdHiss').rand(100000,999999);
		
		$res=Db::table("zz_bill_order")->insert($data);
		var_dump($res);
		
	}*/
			

}
